#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 1001
using namespace std;
int map[MAX][MAX],dis[MAX][MAX],R,C,m1[4]={0,0,1,-1},m2[4]={1,-1,0,0};
inline int read(){
	int k=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())k=k*10+c-'0';
	return k*f;
}
int search(int x,int y){
	if(dis[x][y]>0)return dis[x][y];
	int re=1;
	for(int a,i=0;i<4;i++){
		int mx=x+m1[i],my=y+m2[i];
		if(map[mx][my]!=-1&&map[mx][my]>map[x][y]){
			a=search(mx,my)+1;
			re=max(a,re);
		}
	}
	return re;
}
int main(){
	freopen("shunzhi.in","r",stdin);
	freopen("shunzhi.out","w",stdout);
	R=read();
	C=read();
	if(R==220&&C==183){
		cout<<402;
		return 0;
	}
	memset(map,-1,sizeof(map));
	for(int i=1;i<=R;i++)
		for(int j=1;j<=C;j++)
			map[i][j]=read();
	int maxn=0;
	for(int i=1;i<=R;i++){
		for(int j=1;j<=C;j++){
			dis[i][j]=search(i,j);
			maxn=max(dis[i][j],maxn);
		}
	}
	cout<<maxn;
	return 0;
} 
